<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="util.*"%>
<%@page import="dal.*"%>
<%@ page import="model.*,java.util.*"%>
<%
	boolean hasProject = false;
	int idProject = MiscUtil.getIntUrlParam(request, "idProject");
	String projectName = "All Projects";
	String projectUrl = "projectList.jsp";
	if (idProject != Integer.MIN_VALUE) {
		projectName = CacheUtil.getProjectName(idProject);
		projectUrl = "project.jsp?id=" + idProject;
		hasProject = true;
	}

	GenericDal<EventModel, EventModelExt> dal = new GenericDal<EventModel, EventModelExt>(new EventModel());
	EventModelExt queryCount = new EventModelExt();
	if (idProject != 0) 
		queryCount.setIdProject(idProject);
	int count = dal.count(queryCount);

// 	int pageIndex = 1;
// 	if (request.getParameter("pageIndex") != null && !request.getParameter("pageIndex").isEmpty()) {
// 		try {
// 			pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
// 		} catch (Exception e) {
// 		}
// 	}
	int pageIndex = MiscUtil.getIntUrlParam(request, "pageIndex", 1);
// 	int pageSize = 10;
// 	if (request.getParameter("pageSize") != null && !request.getParameter("pageSize").isEmpty()) {
// 		try {
// 			pageSize = Integer.parseInt(request.getParameter("pageSize"));
// 		} catch (Exception e) {
// 		}
// 	}
	int pageSize = MiscUtil.getIntUrlParam(request, "pageSize", 10);
	
	int noOfPage = count / pageSize;
	if (count % pageSize > 0) {
		noOfPage++;
	}

	EventModelExt query = new EventModelExt();
	if (hasProject == true)
	{
		query.setIdProject(idProject);
	}
	query.setLimit(pageSize);
	query.setOffset((pageIndex - 1) * pageSize);
	List<EventModel> list = dal.selectList(query);

	boolean embedded = false;
	if (request.getParameter("embedded") != null && !request.getParameter("embedded").isEmpty()) {

		if (request.getParameter("embedded").equalsIgnoreCase("true") || request.getParameter("embedded").equalsIgnoreCase("1")) {
			embedded = true;
		}
	}
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Event List</title>

<%if(!embedded){ %>
		<link rel="stylesheet" href="css/jquery-ui-1.8.14.custom.css" type="text/css" />
		<link rel="stylesheet" href="css/themes/ui-lightness/jquery.ui.all.css" />
		<link rel="stylesheet" href="css/jquery-dialog.css" />
		
		<script language="javascript" type="text/javascript" src="js/jquery.min.js"></script>
		<script type="text/javascript" src="js/jquery-ui-1.8.14.custom.min.js"></script>
		<script language="javascript" type="text/javascript" src="js/common.js"></script>
	<%} %> 


<link rel="stylesheet" href="css/listForm.css" />
<link rel="stylesheet" href="css/common.css" />

</head>
<body>
	<%
		if (!embedded) {
	%>
	<div class="divContainer">	
		<jsp:include page="menu.jsp"></jsp:include>
		<br />
		<b>Project: <a id="link_project" name="link_project" href="<%=projectUrl%>"><%=projectName%></a></b>
		<br />
		<br />
		<a style="font-weight: bold;" href="eventNew.jsp?idProject=<%=idProject%>">Add New Event</a>
		<br />
		<a style="font-weight: bold;" href="projectList.jsp">Go Up to Project List</a>		
		<br />
		<br />
	<% } %>
	
		
		<div class="divListHeaderBar">NUMBER OF EVENTS: <%=count%></div>	
		<table class="tblList">
			<tr class="tblListHeader">
				<th class="thListHeader functionColumnNormal">Event</th>
				<th class="thListHeader Id">Id</th>
				<th class="thListHeader">Name</th>
				<th class="thListHeader">Code</th> 
				<th class="thListHeader">Place</th>
				<th class="thListHeader">Venue</th>
				<th class="thListHeader">Description</th>
				
			<% if (!hasProject) { %>
				<th class="thListHeader">Project</th>
			<% } %>
				
			</tr>
	
			<%
				int size = list.size();
				for (int i = 0; i < size; i++) {
					EventModel obj = list.get(i);
			%>
	
			<tr class="trListItem">
				<td class="tdListInt">
				<span><%=i + 1%></span>
				<br/>
				<span class="spanEdit"><a href="event?mode=edit&id=<%=obj.getId()%>">Edit</a></span>
				<br/>
				<span class="spanEdit"><a class="hrefDelete" href="event?mode=delete&id=<%=obj.getId()%>"  onclick="confirmToDelete();">Delete</a></span>
				</td>
				
				<td class="tdList"><%=obj.getId()%></td>
				<td class="tdListText projectName"><%=obj.getNameToString()%></td>
				<td class="tdListText"><%=obj.getCodeToString()%></td>
				<td class="tdListInt"><%=CacheUtil.getPlaceName(obj.getIdPlace())%></td>
				<td class="tdListInt"><%=CacheUtil.getVenueName(obj.getIdVenue())%></td>
				<td class="tdListTextarea"><%=obj.getDescriptionToString()%></td>
				
			<% if (!hasProject) { %>
				<td class="tdListInt"><%=CacheUtil.getProjectName(obj.getIdProject())%></td>
			<% } %>

	
				
			</tr>
			<%
				}
			%>
		</table>
		
		<br />
		<% if (noOfPage > 1) { %>
		<table class="tblPaging">
			<tr class="trPaging">
				<td class="tdPaging">
					<%
						for (int i = 1; i <= noOfPage; i++) {
							if (i == pageIndex) {
					%> <span class="lblPaging"><%=i%></span> <%
					 		} else {
					%> <a class="hrefPaging" href="eventList.jsp?pageIndex=<%=i%>&pageSize=<%=pageSize%>"><%=i%></a> <%
					 		}
					 	}
					%>
				</td>
			</tr>
		</table>
		<% } %>
	<%if(!embedded){ %> </div> <%} %>
</body>
<script language="javascript" type="text/javascript">
// 		$(document).ready(pageLoaded);
// 		function pageLoaded()
// 		{	
// 			initDeleteConfirmation();
// 		} 
	initDeleteConfirmation();
</script>
</html>